package com.panfeng.xcloud.boss.provider.assets.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.panfeng.xcloud.boss.provider.assets.dto.request.*;
import com.panfeng.xcloud.boss.provider.assets.dto.response.*;
import com.panfeng.xcloud.boss.provider.assets.service.IUserQuantOperateService;
import com.panfeng.xcloud.boss.provider.assets.web.vo.request.*;
import com.panfeng.xcloud.boss.provider.assets.web.vo.response.*;
import com.panfeng.xcloud.boss.provider.dto.request.BatchOperateBotReqDTO;
import com.panfeng.xcloud.boss.provider.dto.request.OperateBotReqDTO;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.common.core.web.vo.ResponseVO;
import com.panfeng.xcloud.common.security.annotation.MethodValidate;
import com.panfeng.xcloud.common.security.annotation.NotNeedAccess;
import com.panfeng.xcloud.common.security.web.support.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户量化操作模块
 *
 * @author xiaobo
 * @version 1.0
 * @since 2019-08-08
 */
//@ApiIgnore
@RestController
@RequestMapping("assets/quantOperate")
@Api(tags = {"用户量化操作模块"})
@Slf4j
public class UserQuantOperateController extends BaseController {

    @Autowired
    private IUserQuantOperateService iUserQuantOperateService;

    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/operateBot", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "操作--开仓,清仓,锁仓,解锁,停止机器人", httpMethod = "POST", notes = "操作--开仓,清仓,锁仓,解锁,停止机器人")
    public ResponseVO<OperateBotRespVO> operateBot(@RequestBody @Valid OperateBotReqVO operateBotReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前operateBot请求参数：{} <<<", (null != operateBotReqVO ? JSONObject.toJSONString(operateBotReqVO) : ""));
        OperateBotReqDTO operateBotReqDTO = ConvertUtils.convert(operateBotReqVO, OperateBotReqDTO.class);
        OperateBotRespDTO operateBotRespDTO = iUserQuantOperateService.operateBot(operateBotReqDTO);
        OperateBotRespVO operateBotRespVO = ConvertUtils.convert(operateBotRespDTO, OperateBotRespVO.class);
        log.info(">>>> 当前operateBot响应内容：{} <<<", (null != operateBotRespVO ? JSONObject.toJSONString(operateBotRespVO) : ""));
        return ResponseVO.with(operateBotRespVO);
    }

    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/batchOperateBot", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "批量操作--开仓,清仓,锁仓,解锁,停止机器人", httpMethod = "POST", notes = "批量操作--开仓,清仓,锁仓,解锁,停止机器人")
    public ResponseVO<BatchOperateBotRespVO> batchOperateBot(@RequestBody @Valid BatchOperateBotReqVO batchOperateBotReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前batchOperateBot请求参数：{} <<<", (null != batchOperateBotReqVO ? JSONObject.toJSONString(batchOperateBotReqVO) : ""));
        BatchOperateBotReqDTO batchOperateBotReqDTO = ConvertUtils.convert(batchOperateBotReqVO, BatchOperateBotReqDTO.class);
        BatchOperateBotRespDTO batchOperateBotRespDTO = iUserQuantOperateService.batchOperateBot(batchOperateBotReqDTO);
        BatchOperateBotRespVO batchOperateBotRespVO = ConvertUtils.convert(batchOperateBotRespDTO, BatchOperateBotRespVO.class);
        log.info(">>>> 当前batchOperateBot响应内容：{} <<<", (null != batchOperateBotRespVO ? JSONObject.toJSONString(batchOperateBotRespVO) : ""));
        return ResponseVO.with(batchOperateBotRespVO);
    }

    /**
     * 更新币种交易设置
     *
     * @param updateStrategyReqVO
     * @param bindingResult
     * @return
     */
    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/updateStrategy", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "更新币种交易设置", httpMethod = "POST", notes = "更新币种交易设置")
    public ResponseVO<UpdateStrategyRespVO> updateStrategy(@RequestBody @Valid UpdateStrategyReqVO updateStrategyReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前updateStrategy请求参数：{} <<<", (null != updateStrategyReqVO ? JSONObject.toJSONString(updateStrategyReqVO) : ""));
        UpdateStrategyReqDTO dto = ConvertUtils.convert(updateStrategyReqVO, UpdateStrategyReqDTO.class);
        UpdateStrategyRespDTO updateStrategyRespDTO = iUserQuantOperateService.updateStrategy(dto);
        UpdateStrategyRespVO updateStrategyRespVO = ConvertUtils.convert(updateStrategyRespDTO, UpdateStrategyRespVO.class);
        log.info(">>>> 当前updateStrategy响应内容：{} <<<", (null != updateStrategyRespVO ? JSONObject.toJSONString(updateStrategyRespVO) : ""));
        return ResponseVO.with(updateStrategyRespVO);
    }

    /**
     * 批量更新币种交易设置
     *
     * @param batchUpdateStrategyReqVO
     * @param bindingResult
     * @return
     */
    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/batchUpdateStrategy", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "批量更新币种交易设置", httpMethod = "POST", notes = "批量更新币种交易设置")
    public ResponseVO<BatchUpdateStrategyRespVO> batchUpdateStrategy(@RequestBody @Valid BatchUpdateStrategyReqVO batchUpdateStrategyReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前batchUpdateStrategy请求参数：{} <<<", (null != batchUpdateStrategyReqVO ? JSONObject.toJSONString(batchUpdateStrategyReqVO) : ""));
        BatchUpdateStrategyReqDTO dto = ConvertUtils.convert(batchUpdateStrategyReqVO, BatchUpdateStrategyReqDTO.class);
        BatchUpdateStrategyRespDTO batchUpdateStrategyRespDTO = iUserQuantOperateService.batchUpdateStrategy(dto);
        BatchUpdateStrategyRespVO batchUpdateStrategyRespVO = ConvertUtils.convert(batchUpdateStrategyRespDTO, BatchUpdateStrategyRespVO.class);
        log.info(">>>> 当前batchUpdateStrategy响应内容：{} <<<", (null != batchUpdateStrategyRespVO ? JSONObject.toJSONString(batchUpdateStrategyRespVO) : ""));
        return ResponseVO.with(batchUpdateStrategyRespVO);
    }

    /**
     * 查询币种交易设置
     *
     * @param queryStrategyReqVO
     * @param bindingResult
     * @return
     */
    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/queryStrategy", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询币种交易设置", httpMethod = "POST", notes = "查询币种交易设置")
    public ResponseVO<QueryStrategyRespVO> queryStrategy(@RequestBody @Valid QueryStrategyReqVO queryStrategyReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前queryStrategy请求参数：{} <<<", (null != queryStrategyReqVO ? JSONObject.toJSONString(queryStrategyReqVO) : ""));
        QueryStrategyReqDTO queryStrategyReqDTO = ConvertUtils.convert(queryStrategyReqVO, QueryStrategyReqDTO.class);
        QueryStrategyRespDTO queryStrategyRespDTO = iUserQuantOperateService.queryStrategy(queryStrategyReqDTO);
        QueryStrategyRespVO queryStrategyRespVO = ConvertUtils.convert(queryStrategyRespDTO, QueryStrategyRespVO.class);
        log.info(">>>> 当前queryStrategy响应内容：{} <<<", (null != queryStrategyRespVO ? JSONObject.toJSONString(queryStrategyRespVO) : ""));
        return ResponseVO.with(queryStrategyRespVO);
    }

    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/getOpeningQuantBotList", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取已开启量化的交易对列表", httpMethod = "POST", notes = "获取已开启量化的交易对列表")
    public ResponseVO<UserQuantBotRespVO> getOpeningQuantBotList(@RequestBody @Valid OpenQuantBotReqVO openQuantBotReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前getOpeningQuantBotList请求参数：{} <<<", (null != openQuantBotReqVO ? JSONObject.toJSONString(openQuantBotReqVO) : ""));
        OpenQuantBotReqDTO openQuantBotReqDTO = ConvertUtils.convert(openQuantBotReqVO, OpenQuantBotReqDTO.class);
        UserQuantBotRespVO userQuantBotRespVO = iUserQuantOperateService.getOpeningQuantBotList(openQuantBotReqDTO);
        log.info(">>>> 当前openQuantBotRespVO响应内容：{} <<<", (null != userQuantBotRespVO ? JSONObject.toJSONString(userQuantBotRespVO) : ""));
        return ResponseVO.with(userQuantBotRespVO);
    }

    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/getOpeningQuantBotDetail", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取已开启量化的交易对详情", httpMethod = "POST", notes = "获取已开启量化的交易对详情")
    public ResponseVO<OpenQuantBotDetailRespVO> getOpeningQuantBotDetail(@RequestBody @Valid OpenQuantBotDetailReqVO openQuantBotDetailReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前getOpeningQuantBotDetail请求参数：{} <<<", (null != openQuantBotDetailReqVO ? JSONObject.toJSONString(openQuantBotDetailReqVO) : ""));
        OpenQuantBotDetailReqDTO openQuantBotDetailReqDTO = ConvertUtils.convert(openQuantBotDetailReqVO, OpenQuantBotDetailReqDTO.class);
        OpenQuantBotDetailRespDTO openQuantBotDetailRespDTO = iUserQuantOperateService.getOpeningQuantBotDetail(openQuantBotDetailReqDTO);
        OpenQuantBotDetailRespVO openQuantBotDetailRespVO = ConvertUtils.convert(openQuantBotDetailRespDTO, OpenQuantBotDetailRespVO.class);
        log.info(">>>> 当前getOpeningQuantBotDetail响应内容：{} <<<", (null != openQuantBotDetailRespVO ? JSONObject.toJSONString(openQuantBotDetailRespVO) : ""));
        return ResponseVO.with(openQuantBotDetailRespVO);
    }

    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/getSysOpeningQuantBotList", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取全部的交易对列表", httpMethod = "POST", notes = "获取全部的交易对列表")
    public ResponseVO<List<GetSysOpeningQuantBotListRespVO>> getSysOpeningQuantBotList(@RequestBody @Valid GetSysOpeningQuantBotListReqVO getSysOpeningQuantBotListReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前getSysOpeningQuantBotList请求参数：{} <<<", (null != getSysOpeningQuantBotListReqVO ? JSONObject.toJSONString(getSysOpeningQuantBotListReqVO) : ""));
        GetSysOpeningQuantBotListReqDTO getSysOpeningQuantBotListReqDTO = ConvertUtils.convert(getSysOpeningQuantBotListReqVO, GetSysOpeningQuantBotListReqDTO.class);
        List<GetSysOpeningQuantBotListRespDTO> getSysOpeningQuantBotListRespDTO = iUserQuantOperateService.getSysOpeningQuantBotList(getSysOpeningQuantBotListReqDTO);
        List<GetSysOpeningQuantBotListRespVO> openQuantBotRespVOS = ConvertUtils.convert(getSysOpeningQuantBotListRespDTO, GetSysOpeningQuantBotListRespVO.class);
        log.info(">>>> 当前openQuantBotRespVO响应内容：{} <<<", (openQuantBotRespVOS.size() > 0 ? JSONObject.toJSONString(openQuantBotRespVOS) : ""));
        return ResponseVO.with(openQuantBotRespVOS);
    }

    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/setStopLossNum", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "设置当前止损次数", httpMethod = "POST", notes = "设置当前止损次数")
    public ResponseVO<SetStopLossNumRespVO> setStopLossNum(@RequestBody @Valid SetStopLossNumReqVO setStopLossNumReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前setStopLossNum请求参数：{} <<<", (null != setStopLossNumReqVO ? JSONObject.toJSONString(setStopLossNumReqVO) : ""));
        SetStopLossNumReqDTO setStopLossNumReqDTO = ConvertUtils.convert(setStopLossNumReqVO, SetStopLossNumReqDTO.class);
        SetStopLossNumRespDTO setStopLossNumRespDTO = iUserQuantOperateService.setStopLossNum(setStopLossNumReqDTO);
        SetStopLossNumRespVO setStopLossNumRespVO = ConvertUtils.convert(setStopLossNumRespDTO, SetStopLossNumRespVO.class);
        log.info(">>>> 当前setStopLossNum响应内容：{} <<<", (null != setStopLossNumRespVO ? JSONObject.toJSONString(setStopLossNumRespVO) : ""));
        return ResponseVO.with(setStopLossNumRespVO);
    }

    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/getSmallestInitOrderPosition", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取首单金额的最低金额", httpMethod = "POST", notes = "获取首单金额的最低金额")
    public ResponseVO<GetSmallestInitOrderPositionRespVO> getSmallestInitOrderPosition(@RequestBody @Valid GetSmallestInitOrderPositionReqVO getSmallestInitOrderPositionReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前getSmallestInitOrderPosition请求参数：{} <<<", (null != getSmallestInitOrderPositionReqVO ? JSONObject.toJSONString(getSmallestInitOrderPositionReqVO) : ""));
        GetSmallestInitOrderPositionReqDTO getSmallestInitOrderPositionReqDTO = ConvertUtils.convert(getSmallestInitOrderPositionReqVO, GetSmallestInitOrderPositionReqDTO.class);
        GetSmallestInitOrderPositionRespDTO getSmallestInitOrderPositionRespDTO = iUserQuantOperateService.getSmallestInitOrderPosition(getSmallestInitOrderPositionReqDTO);
        GetSmallestInitOrderPositionRespVO getSmallestInitOrderPositionRespVO = ConvertUtils.convert(getSmallestInitOrderPositionRespDTO, GetSmallestInitOrderPositionRespVO.class);
        log.info(">>>> 当前getSmallestInitOrderPosition响应内容：{} <<<", (null != getSmallestInitOrderPositionRespVO ? JSONObject.toJSONString(getSmallestInitOrderPositionRespVO) : ""));
        return ResponseVO.with(getSmallestInitOrderPositionRespVO);
    }

    /**
     * 管理员更新币种交易设置
     *
     * @param updateStrategyByAdminReqVO
     * @param bindingResult
     * @return
     */
    @NotNeedAccess
    @MethodValidate
    @PostMapping(value = "/updateStrategyByAdmin", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "管理员更新币种交易设置", httpMethod = "POST", notes = "管理员更新币种交易设置")
    public ResponseVO<UpdateStrategyByAdminRespVO> updateStrategyByAdmin(@RequestBody @Valid UpdateStrategyByAdminReqVO updateStrategyByAdminReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前updateStrategyByAdmin请求参数：{} <<<", (null != updateStrategyByAdminReqVO ? JSONObject.toJSONString(updateStrategyByAdminReqVO) : ""));
        UpdateStrategyByAdminReqDTO dto = ConvertUtils.convert(updateStrategyByAdminReqVO, UpdateStrategyByAdminReqDTO.class);
        UpdateStrategyByAdminRespDTO updateStrategyByAdminRespDTO = iUserQuantOperateService.updateStrategyByAdmin(dto);
        UpdateStrategyByAdminRespVO updateStrategyByAdminRespVO = ConvertUtils.convert(updateStrategyByAdminRespDTO, UpdateStrategyByAdminRespVO.class);
        log.info(">>>> 当前updateStrategyByAdmin响应内容：{} <<<", (null != updateStrategyByAdminRespVO ? JSONObject.toJSONString(updateStrategyByAdminRespVO) : ""));
        return ResponseVO.with(updateStrategyByAdminRespVO);
    }

}
